# == Schema Information
# Schema version: 36
#
# Table name: articles
#
#  id         :integer       not null, primary key
#  title      :string(255)   not null
#  body       :text          not null
#  author     :string(255)   not null
#  created_at :datetime      not null
#  updated_at :datetime      not null
#  blurb      :text          not null
#

# An Article  represents a single article in a newspaper, so that means
# that it's got:
#   * a title
#   * an author
#   * a date
#   * the full text of the article, possibly including links to images
#   * a blurb

class Article < ActiveRecord::Base


  has_and_belongs_to_many :sections
  

  validates_presence_of :title, :blurb, :body, :author
# Minimum and Maximum should be configurable
  validates_length_of :title, :within=> 3..400
  validates_length_of :blurb, :within=> 3..1500
  validates_length_of :body, :minimum=> 30
  validates_length_of :author, :minimum=> 3

# TODO: Find a way to make it only for the scopeof some sections, maybe using before_save
  validates_uniqueness_of :title



  def self.find_by_title(possible_title)
    
    #     Trying to prevent SQL injection,
    #     comment this line and try to look for this article title:
    #     );DELETE from article_sources;( SELECT * from article_sources where title like
    #     then you will delete all the article sources!

    # Accepting only alphanumeric stuff and spaces
    unless possible_title=~/\A(\s*\w*\s*)*\Z/
      possible_title = ""
    end
    # Extra paranoind gsub just in case 
    possible_title = possible_title.gsub(/;|,|'|\*|"|\)|\(/,'')
    self.find :all, :conditions => ["title ~* ? ", possible_title]
    
  end

  def self.find_by_all(query)
    # Prevent SQL injection like above
    unless query=~/\A(\s*\w*\s*)*\Z/
      query = ""
    end
    q = query.gsub(/;|,|'|\*|"|\)|\(/,'')
    
    # We should really be using the FULLTEXT and MATCH sql calls here to increase performance
    results = Article.find(:all, :conditions => ["body ~* ? OR author ~* ? OR title ~* ? OR blurb ~* ?", q, q, q, q])
  end
  
  

#   Making sure that no html is being saved
#   FIXME: The html escaping breaks the textile
#   >> indentation !
  def before_save

    self.body = ERB::Util.h(self.body)
    self.title = ERB::Util.h(self.title)
    self.blurb = ERB::Util.h(self.blurb)
    self.author = ERB::Util.h(self.author)

  end
  
  def before_destroy
    #Destroy associations
  end

# Return the sections this article is accesible from, (that's whe the issue a particular section belongs to answers yes to its show method)
  def readable_from(date)
    sec = []
    for section in self.sections
        if ! section.issue.nil? and section.issue.show?(date)
            sec << section
        end
    end
    sec
  end


end



##   TODO:There is a better way to declare an interval in ruby
#  @@priorities = [1,2,3,4,5,6,7,8,9].freeze
#
#  validates_inclusion_of :priority, :in => @@priorities
#  validates_presence_of :section_id
#  validates_presence_of :article_source_id
##   Let the others kown the possible priorities
#  def self.possible_priorities
#    @@priorities
#  end
#
#  def self.find_by_section(id)
#    self.find(:all, :conditions=> [" section_id = ? ", id])
#  end
